package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.CommunityReportDTO;
import com.ruoyi.system.domain.vo.CommunityReportVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CommunityReportMapper;
import com.ruoyi.system.domain.CommunityReport;
import com.ruoyi.system.service.ICommunityReportService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * reportService业务层处理
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@Service
public class CommunityReportServiceImpl implements ICommunityReportService {
    @Resource
    private CommunityReportMapper communityReportMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询report
     *
     * @param reportId report主键
     * @return report
     */
    @Override
    public List<CommunityReport> selectCommunityReportByReportId(Long reportId) {
        return communityReportMapper.selectCommunityReportByReportId(reportId);
    }

    /**
     * 查询report列表
     *
     * @param communityReport report
     * @return report
     */
    @Override
    public List<CommunityReport> selectCommunityReportList(CommunityReport communityReport) {
        return communityReportMapper.selectCommunityReportList(communityReport);
    }

    /**
     * 新增report
     *
     * @param communityReport report
     * @return 结果
     */
    @Override
    public int insertCommunityReport(CommunityReport communityReport) {
        communityReport.setCreateTime(DateUtils.getNowDate());
        return communityReportMapper.insertCommunityReport(communityReport);
    }


    /**
     * 修改report
     *
     * @param communityReport report
     * @return 结果
     */
    @Override
    public int updateCommunityReport(CommunityReport communityReport) {
        communityReport.setUpdateTime(DateUtils.getNowDate());
        return communityReportMapper.updateCommunityReport(communityReport);
    }


    /**
     * 修改维修工
     * 想做的事就是如果状态是待处理的话才能点击完成。
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCommunityReportUserId(CommunityReportDTO communityReportDto) {
        communityReportDto.setUpdateTime(DateUtils.getNowDate());
        // 将未指派状态变成待处理
        communityReportDto.setStatus(String.valueOf(1));
        // 切换维修工
        communityReportMapper.updateCommunityReportUserId(communityReportDto);
        // assignor:当前用户的名字就是指派的人
        String assignor = SecurityUtils.getLoginUser().getUser().getUserName();
        SysUser sysUser = userService.selectUserById(Long.valueOf(communityReportDto.getUserId()));
        // assignee: 就是被指派人
        String assignee = sysUser.getUserName();
        String str = assignor + "指派" + assignee;
        return communityReportMapper.insertCommunityReportUserId(communityReportDto.getReportId(), str);
    }

    /**
     * 将状态改为完成
     *
     * @param communityReportDtO 报修传输对象
     * @return 结果
     */
    @Override
    public int updateCommunityReportFinish(CommunityReportDTO communityReportDtO) {
        communityReportDtO.setUpdateTime(DateUtils.getNowDate());
        // 将指派状态变成已完成
        communityReportDtO.setStatus(String.valueOf(2));
        // 调用更新SQL
        return communityReportMapper.updateCommunityReportUserId(communityReportDtO);
    }


    /**
     * 批量删除report
     *
     * @param reportIds 需要删除的report主键
     * @return 结果
     */
    @Override
    public int deleteCommunityReportByReportIds(Long[] reportIds) {
        return communityReportMapper.deleteCommunityReportByReportIds(reportIds);
    }

    /**
     * 删除report信息
     *
     * @param reportId report主键
     * @return 结果
     */
    @Override
    public int deleteCommunityReportByReportId(Long reportId) {
        return communityReportMapper.deleteCommunityReportByReportId(reportId);
    }
}
